<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AccountController
 *
 * @author bom
 */

class Admin_User_AccountController extends My_Controller_Action {
	public function init(){
		parent::init();
		
		$auth = Zend_Auth::getInstance();
        
        if(!$auth->hasIdentity())
        { 
            $this->_redirect("user/account/login");
        }
        
        if ($this->isAccess($auth->getIdentity()->group_id, 7))
        {
			//to do
		} else {
            die("Access denied");
        }
	}   
    
	public function indexAction() {
		$this->view->rightAdminModules('right');
		
		$this->view->by = $this->_request->getParam("by", "u.user_id");
        $this->view->order = $this->_request->getParam("order", "desc");
        	
        Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
    	$model = $this->loadModel('admin_user', 'Account');
		$this->view->paginator = $model->getAllUsers($this->_getParam('page', 1), $this->view->by.' '.$this->view->order);
    }

    public function editAction() {
		$this->view->rightAdminModules('right');

		$this->view->errors = $this->_errorMessenger->getMessages();

		$user_id = $this->_request->getParam('id');
		$model = $this->loadModel('admin_user', 'Account');
		$this->view->user = $model->fetchRow($model->select()->where('user_id=?', $user_id));
		$this->view->user_id = $user_id;
		$model = $this->loadModel('admin_user', 'Group');
		$this->view->groups = $model->fetchAll($model->select());
    }

    public function editpostAction() {
		$error = false;
		if ($this->_request->isPost()) {
			$model = $this->loadModel('admin_user', 'Account');

			$user_id = $this->_request->getParam("user_id");
			$user = $model->fetchRow($model->select()->where('user_id=?',$user_id));
			if (!$this->_request->getParam("email")) {
				$error = true;
				$this->_errorMessenger->addMessage("Nháº­p email");
			}
			$email = $this->_request->getParam("email");
			if ($model->isEmailUsed($email) && $email != $user->email) {
				$error = true;
				$this->_errorMessenger->addMessage("Email Ä‘Ã£ Ä‘Æ°á»£c sá»­ dá»¥ng");
			}

			if ($error) {
				$redirect_url = $this->getUrl(array("module" => "admin_user", "controller" => "account", "action" => "edit", "id" => $user_id));
				$this->_redirect($redirect_url);
			} else {
				$user_id = $this->_request->getParam('user_id');
				$user = $model->getUserById($user_id);
				$user->email = $this->_request->getParam('email');
				$user->group_id = $this->_request->getParam('group_id');
				$user->save();
				$redirect_url = $this->getUrl(array("module" => "admin_user", "controller" => "account", "action" => "index"));
				$this->_redirect($redirect_url);
			}
		} else {
			$redirect_url = $this->getUrl(array("module" => "admin_user", "controller" => "account", "action" => "index"));
			$this->_redirect($redirect_url);
		}
    }
    
    public function changepasspostAction() {
		if ($this->_request->isPost()) {
			$model = $this->loadModel('admin_user', 'Account');
			$newpass = $this->_request->getParam('password');
			$user_id = $this->_request->getParam('user_id');
			$user = $model->getUserById($user_id);
			$user->password = md5($newpass);
			$user->save();
		}
		$redirect_url = $this->getUrl(array("module" => "admin_user", "controller" => "account", "action" => "index"));
		$this->_redirect($redirect_url);
    }

    public function deleteAction() {
		$model = $this->loadModel('admin_user', 'Account');
		$user_id = $this->_request->getParam('id');
		$user = $model->getUserById($user_id);
		$user->delete();

		$redirect_url = $this->getUrl(array("module" => "admin_user", "controller" => "account", "action" => "index"));
		$this->_redirect($redirect_url);
    }

    public function addAction() {
		$this->view->rightAdminModules('right');
		$this->view->errors = $this->_errorMessenger->getMessages();
		$this->_errorMessenger->clearMessages();
		$groupModel = $this->loadModel('admin_user', 'Group');
		$this->view->groups = $groupModel->getAllGroup();        
    }

    public function addpostAction() {
		$model = $this->loadModel('admin_user', 'Account');
		$error = false;
		if (!$this->_request->getParam("username")) {
			$error = true;
			$this->_errorMessenger->addMessage("Nhập tên đăng nhập");
		} else {
			if ($model->isUsernameUsed($this->_request->getParam("username"))) {
				$error = true;
				$this->_errorMessenger->addMessage("Tên đăng nhập đã được sử dụng");
			}
		}
		if (!$this->_request->getParam("password")) {
			$error = true;
			$this->_errorMessenger->addMessage("Nhập mật khẩu");
		}
		if (!$this->_request->getParam("password2")) {
			$error = true;
			$this->_errorMessenger->addMessage("Nhập xác nhận");
		}
		if ($this->_request->getParam("password") != $this->_request->getParam("password2")) {
			$error = true;
			$this->_errorMessenger->addMessage("Sai xác nhận");
		}
		if (!$this->_request->getParam("email")) {
			$error = true;
			$this->_errorMessenger->addMessage("Nhập email");
		} else {
			if ($model->isEmailUsed($this->_request->getParam("email"))) {
				$error = true;
				$this->_errorMessenger->addMessage("Email đã được sử dụng");
			}
		}
		if ($error) {
			$this->postData->data = $this->_request->getParams();
			$redirect_url = $this->getUrl(array("module" => "admin_user", "controller" => "account", "action" => "add"));
			$this->_redirect($redirect_url);
		} else {
			$newuser = $model->createrow();
			$newuser->username = $this->_request->getParam("username");
			$newuser->password = md5($this->_request->getParam("password"));
			$newuser->email = $this->_request->getParam("email");
			$newuser->group_id = $this->_request->getParam("group_id");
			$newuser->save();

			$this->postData->data = $this->_request->getParams();
			$redirect_url = $this->getUrl(array("module" => "admin_user", "controller" => "account", "action" => "index"));
			$this->_redirect($redirect_url);
		}
    }
}

?>
